﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Qu.ComputingContest.Model;

namespace Qu.ComputingContest.WebUI.Controllers
{
    public class RegistrationController : Controller
    {

        private void PopulateDropDowns()
        {
            var maleItem = new SelectListItem { Text = "Male", Value = "M" };
            var femaleItem = new SelectListItem { Text = "Female", Value = "F" };
            List<SelectListItem> genderList = new List<SelectListItem>();
            genderList.Add(maleItem);
            genderList.Add(femaleItem);
            var genders = new SelectList(genderList, "Value", "Text");
            ViewBag.Genders = genders;

            var year10 = new SelectListItem { Text = "Year 10", Value = "10" };
            var year11 = new SelectListItem { Text = "Year 11", Value = "11" };
            var year12 = new SelectListItem { Text = "Year 12", Value = "12" };
            List<SelectListItem> schoolYearsList = new List<SelectListItem>();
            schoolYearsList.Add(year10);
            schoolYearsList.Add(year11);
            schoolYearsList.Add(year12);
            var schoolYears = new SelectList(schoolYearsList, "Value", "Text");
            ViewBag.SchoolYears = schoolYears;

            List<SelectListItem> workshopsList = new List<SelectListItem>();
            //Workshop1Date & Workshop2Date
            var workshop1Date = RegistrationRepository.GetConfigValueAsDate("Workshop1Date");
            var workshop2Date = RegistrationRepository.GetConfigValueAsDate("Workshop2Date");

            //Only add the workshop dates that are not passed
            if (workshop1Date >= DateTime.Now)
            {
                var workshop1 = new SelectListItem { Text = workshop1Date.ToString("dddd dd/MM/yyyy"), Value = workshop1Date.ToString("dd MMMM yyyy") };
                workshopsList.Add(workshop1);
            }

            if (workshop2Date >= DateTime.Now)
            {
                var workshop2 = new SelectListItem { Text = workshop2Date.ToString("dddd dd/MM/yyyy"), Value = workshop2Date.ToString("dd MMMM yyyy") };
                workshopsList.Add(workshop2);
            }

            var workshops = new SelectList(workshopsList, "Value", "Text");
            ViewBag.Workshops = workshops;

            TempData["StudentCount"] = 1;
        }

        public ActionResult New()
        {
            ViewBag.RegistrationClosedMessage = RegistrationRepository.GetRegistrationClosedMessage();

            //If the registration is closed then just return.
            if (!string.IsNullOrEmpty(ViewBag.RegistrationClosedMessage))
                return View();


            ViewBag.MaxKitsReachedMessage = RegistrationRepository.GetMaxKitsReachedMessage();
            ViewBag.WorkshopInvitationMessage = RegistrationRepository.GetConfigValue("WorkshopInvitationMessage");
            Registration registration = new Registration(2);
            //registration.WorkshopDate = new DateTime(2011, 3, 19);
            PopulateDropDowns();
            return View(registration);
        }

        [HttpPost]
        public ActionResult New(Registration model)
        {
            if (!ModelState.IsValid)
            {
                PopulateDropDowns();
                return View(model);
            }
            else
            {
                if (RegistrationRepository.IsRegistrationExists(model))
                {
                    ModelState.AddModelError("", "Registration failed because the Students enetred are already registred!");
                    PopulateDropDowns();
                    return View(model);
                }

                RegistrationRepository.AddRegistration(model);
                ViewBag.SuccessMessage = "Thanks for your interest in the Computing Contest. Your registration is received successfully. You will receive a confirmation email shortly (if you do not see a confirmation email within few minutes, please check your Spam folder or Junk mail box).";
                return View(model);
            }
        }

    }
}
